package com.intramirror.wd.business.manage.impl;

import com.intramirror.wd.business.manage.UserManage;
import com.intramirror.wd.business.mapper.TransitUserMapper;
import com.intramirror.wd.business.util.CheckUtils;
import com.intramirror.wd.model.po.TransitUser;
import com.intramirror.wd.model.po.TransitUserExample;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by juzhongzheng on 2018/4/27.
 */
@Service
public class UserManageImpl implements UserManage {

    @Autowired
    private TransitUserMapper transitUserMapper;


    @Override
    public TransitUser findUserByUserName(String userName) {
        if(StringUtils.isBlank(userName)){
            return null;
        }
        TransitUserExample example = new TransitUserExample();
        TransitUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(userName);
        List<TransitUser> users = transitUserMapper.selectByExample(example);
        if(CollectionUtils.isNotEmpty(users)){
            return users.get(0);
        }
        return null;
    }

    @Override
    public void updateUserInfo(TransitUser user) {
        if(user==null){
            return;
        }
        user.setUpdateTime(new Date());
        transitUserMapper.updateByPrimaryKey(user);
    }

    @Override
    public List<TransitUser> findUserByUserIds(List<Long> userIds) {
        if (CheckUtils.isNotEmpty(userIds)) {
            TransitUserExample example = new TransitUserExample();
            TransitUserExample.Criteria criteria = example.createCriteria();
            criteria.andUserIdIn(userIds);
            return transitUserMapper.selectByExample(example);
        }
        return new ArrayList<>();
    }
}
